package config

import (
	"github.com/spf13/viper"
	"jialiking.com/gaogang/common/utils"
	"os"
)

var Config *viper.Viper

var DSN string = `root:badou@2023@tcp(127.0.0.1:3306)/light?charset=utf8mb4&parseTime=True&loc=UTC`

var FASPort int
var ModbusPort int

var DjangoUrl string

var MqttHost string
var MqttUsername string
var MqttPassword string
var MQTTAPIHost string
var MQTTAPIUsername string
var MQTTAPIPassword string

var InfluxUrl string
var InfluxTimeout uint
var InfluxDatabase string
var InfluxPrecision string
var InfluxMeasurement string

var RedisAddr string
var RedisKeyPrefix string

var AMQP string

func init() {
	viper.SetConfigType("yaml")

	viper.AddConfigPath("./config")
	viper.SetConfigName("base")
	err := viper.ReadInConfig()
	if nil != err {
		utils.ErrorLog("init config error: ", err.Error())
		panic(err)
	}
	if env, exist := os.LookupEnv("env"); exist {
		viper.SetConfigName(env)
		err := viper.MergeInConfig()
		if err != nil {
			panic(err)
		}
	}

	Config = viper.GetViper()
	FASPort = Config.GetInt("fas.port")
	ModbusPort = Config.GetInt("modbus.port")
	DjangoUrl = Config.GetString("django.url")
	DSN = Config.GetString("mysql.dsn")
	RedisAddr = Config.GetString("redis.addr")
	//MqttHost = Config.GetString("mqtt.host")
	//MqttUsername = Config.GetString("mqtt.username")
	//MqttPassword = Config.GetString("mqtt.password")
	//MQTTAPIHost = Config.GetString("mqtt.api_host")
	//MQTTAPIUsername = Config.GetString("mqtt.api_username")
	//MQTTAPIPassword = Config.GetString("mqtt.api_password")
	//InfluxUrl = Config.GetString("influx.url")
	//InfluxTimeout = Config.GetUint("influx.timeout")
	//InfluxDatabase = Config.GetString("influx.database")
	//InfluxPrecision = Config.GetString("influx.precision")
	//InfluxMeasurement = Config.GetString("influx.measurement")
	//RedisKeyPrefix = Config.GetString("redis.key_prefix")
	//AMQP = Config.GetString("amqp.addr")
}
